<?php
include("../includes/common.php");

if(!$conf['userlogin']){
    @header('Content-Type: text/html; charset=UTF-8');
	exit("<script language='javascript'>alert('未开启登录');window.location.href='./';</script>");
}
if(isset($_GET['logout'])){
			if(!checkRefererHost())exit();
				setcookie("user_token", "", time() - 1, '/');
				@header('Content-Type: text/html; charset=UTF-8');
                exit('{"code":1,"msg":"注销登录成功！"}');
			}elseif($islogin2==1){
				@header('Content-Type: text/html; charset=UTF-8');
				exit("<script language='javascript'>alert('您已登录！');window.location.href='./';</script>");
			}elseif(isset($_GET['act']) && $_GET['act']=='connect'){
				@header('Content-Type: application/json; charset=UTF-8');
				$type = isset($_POST['type'])?$_POST['type']:exit('{"code":-1,"msg":"no type"}');
				if(!$conf['login_apiurl'] || !$conf['login_appid'] || !$conf['login_appkey'])exit('{"code":-1,"msg":"未配置好快捷登录接口信息"}');
				$Oauth = new \lib\Oauth($conf['login_apiurl'], $conf['login_appid'], $conf['login_appkey']);
				$res = $Oauth->login($type);
				if(isset($res['code']) && $res['code']==0){
					$result = ['code'=>0, 'url'=>$res['url']];
				}elseif(isset($res['code'])){
					$result = ['code'=>-1, 'msg'=>$res['msg']];
				}else{
					$result = ['code'=>-1, 'msg'=>'快捷登录接口请求失败'];
				}
				exit(json_encode($result));
			}elseif($_GET['code'] && $_GET['type'] && $_GET['state']){
                
				// 清除可能存在的用户阻止标记
				unset($_SESSION['user_block']);
				
				if($_GET['state'] != $_SESSION['Oauth_state']){
					sysmsg("<h2>The state does not match. You may be a victim of CSRF.</h2>");
				}
				$type = $_GET['type'];
				$typename = $type=='wx'?'微信':'QQ';
				$Oauth = new \lib\Oauth($conf['login_apiurl'], $conf['login_appid'], $conf['login_appkey']);
				$arr = $Oauth->callback();
				if(isset($arr['code']) && $arr['code']==0){
					$openid=$arr['social_uid'];
					$access_token=$arr['access_token'];
					$nickname=trim($arr['nickname']);
					if(empty($nickname) || $nickname=='-') $nickname = $typename.'用户';
					$faceimg=$arr['faceimg'];
				}elseif(isset($arr['code'])){
					sysmsg('<h3>error:</h3>'.$arr['errcode'].'<h3>msg  :</h3>'.$arr['msg']);
				}else{
					sysmsg('获取登录数据失败');
				}

				$userrow=$DB->find('user','*',['type'=>$type, 'openid'=>$openid], null, '1');
				if(!$userrow){
                    if(!$conf['login_zc'] || !$conf['login_zc'] || !$conf['login_zc'])exit('{"code":-1,"msg":"当前暂停注册！"}');
					// 新用户注册逻辑 - 不应该检查 user_block
					if(!$DB->insert('user', [
						'type' => $type,
						'openid' => $openid,
						'nickname' => $nickname,
						'faceimg' => $faceimg,
						'enable' => 1,
						'regip' => $clientip,
						'loginip' => $clientip,
						'addtime' => 'NOW()',
						'lasttime' => 'NOW()',
					]))sysmsg('用户注册失败 '.$DB->error());
					$uid = $DB->lastInsertId();
				}else{
					// 已存在用户 - 检查是否被禁用
					if($userrow['enable']==0){
						// 设置阻止标记，但不立即退出，让后续逻辑处理
						$_SESSION['user_block'] = true;
						sysmsg('当前用户已被禁止登录');
					}
					$uid = $userrow['uid'];
					$DB->update('user', ['loginip' => $clientip, 'lasttime'=>'NOW()'], ['uid'=>$uid]);
				}
				
				// 检查阻止标记 - 只有已存在用户且被禁用时才触发
				if(isset($_SESSION['user_block']) && $_SESSION['user_block']){
					// 如果是新用户，不应该执行到这里
					if(isset($userrow) && $userrow['enable']==0){
						$DB->update('user', ['enable' => 0], ['uid'=>$uid]);
					}
					sysmsg('当前用户已被禁止登录');
				}
				
				// 处理用户文件
				if(isset($_SESSION['fileids']) && count($_SESSION['fileids'])>0){
					$ids = array_reverse($_SESSION['fileids']);
					if(count($ids) > 60){
						$ids = array_splice($ids, 0, 60);
					}
					$ids = implode(',',$ids);
					$DB->exec("UPDATE pre_file SET uid='{$uid}' WHERE id IN ({$ids}) AND uid=0");
				}
				
				$session=md5($type.$openid.$password_hash);
				$expiretime=time()+2592000;
				$token=authcode("{$uid}\t{$session}\t{$expiretime}", 'ENCODE', SYS_KEY);
				ob_clean();
				setcookie("user_token", $token, time() + 2592000, '/');
				
				// 登录成功，清除所有会话标记
				unset($_SESSION['user_block']);
				unset($_SESSION['Oauth_state']);
				
				exit("<script language='javascript'>window.location.href='./';</script>");
			}

?>
  <!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录 - <?php echo $conf['name']; ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/layer/2.3/skin/layer.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3a0ca3;
            --accent-color: #4cc9f0;
            --light-color: #f8f9fa;
            --dark-color: #212529;
            --success-color: #4bb543;
            --warning-color: #ffcc00;
            --danger-color: #dc3545;
            --gray-color: #6c757d;
            --border-radius: 12px;
            --box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s ease;
        }
        
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .login-container {
            width: 100%;
            max-width: 450px;
            margin: 0 auto;
        }
        
        .login-card {
            background: white;
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            overflow: hidden;
            transition: var(--transition);
        }
        
        .login-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
        }
        
        .login-header {
            padding: 30px 30px 20px;
            text-align: center;
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            position: relative;
        }
        
        .login-title {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 8px;
        }
        
        .login-sub {
            font-size: 14px;
            opacity: 0.9;
            margin-bottom: 10px;
        }
        
        .home-btn {
            position: absolute;
            top: 20px;
            right: 20px;
            color: white;
            background: rgba(255, 255, 255, 0.2);
            border: none;
            border-radius: 50%;
            width: 36px;
            height: 36px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: var(--transition);
            text-decoration: none;
        }
        
        .home-btn:hover {
            background: rgba(255, 255, 255, 0.3);
            transform: scale(1.1);
        }
        
        .login-tabs {
            display: flex;
            border-bottom: 1px solid #e9ecef;
        }
        
        .login-tab {
            flex: 1;
            text-align: center;
            padding: 16px 0;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            color: var(--gray-color);
            border-bottom: 3px solid transparent;
        }
        
        .login-tab.active {
            color: var(--primary-color);
            border-bottom: 3px solid var(--primary-color);
        }
        
        .login-tab:hover {
            background-color: #f8f9fa;
        }
        
        .login-content {
            padding: 30px;
        }
        
        .tab-content {
            display: none;
        }
        
        .tab-content.active {
            display: block;
            animation: fadeIn 0.5s ease;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .social-login {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-bottom: 25px;
        }
        
        .social-btn {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: white;
            cursor: pointer;
            transition: var(--transition);
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }
        
        .social-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 15px rgba(0, 0, 0, 0.15);
        }
        
        .social-btn.qq {
            background: linear-gradient(135deg, #12b7f5, #0084ff);
        }
        
        .social-btn.wx {
            background: linear-gradient(135deg, #09bb07, #07a907);
        }
        
        .social-btn.email {
            background: linear-gradient(135deg, #ff6b6b, #ee5a52);
        }
        
        .social-label {
            text-align: center;
            font-size: 14px;
            color: var(--gray-color);
            margin-bottom: 25px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: var(--dark-color);
        }
        
        .form-control {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ced4da;
            border-radius: 8px;
            font-size: 16px;
            transition: var(--transition);
        }
        
        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.2);
            outline: none;
        }
        
        .form-options {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        
        .checkbox-group {
            display: flex;
            align-items: center;
        }
        
        .checkbox-group input {
            margin-right: 8px;
        }
        
        .forgot-link {
            color: var(--primary-color);
            text-decoration: none;
            font-size: 14px;
            transition: var(--transition);
        }
        
        .forgot-link:hover {
            text-decoration: underline;
        }
        
        .login-btn {
            width: 100%;
            padding: 14px;
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            box-shadow: 0 4px 15px rgba(67, 97, 238, 0.3);
        }
        
        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(67, 97, 238, 0.4);
        }
        
        .login-btn:active {
            transform: translateY(0);
        }
        
        .register-prompt {
            text-align: center;
            margin-top: 20px;
            font-size: 14px;
            color: var(--gray-color);
        }
        
        .register-link {
            color: var(--primary-color);
            text-decoration: none;
            font-weight: 600;
            transition: var(--transition);
        }
        
        .register-link:hover {
            text-decoration: underline;
        }
        
        .login-footer {
            text-align: center;
            padding: 20px;
            font-size: 12px;
            color: var(--gray-color);
            border-top: 1px solid #e9ecef;
            background: #f8f9fa;
        }
        
        /* 移动端优化 */
        @media (max-width: 576px) {
            .login-header {
                padding: 25px 20px 15px;
            }
            
            .login-content {
                padding: 20px;
            }
            
            .social-btn {
                width: 50px;
                height: 50px;
                font-size: 20px;
            }
            
            .login-title {
                font-size: 20px;
            }
            
            .login-sub {
                font-size: 13px;
            }
        }
        
        /* 超小屏幕优化 */
        @media (max-width: 375px) {
            .social-login {
                gap: 15px;
            }
            
            .social-btn {
                width: 45px;
                height: 45px;
                font-size: 18px;
            }
            
            .login-content {
                padding: 15px;
            }
        }
        
        /* 平板优化 */
        @media (min-width: 768px) and (max-width: 1024px) {
            .login-container {
                max-width: 500px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-card">
            <div class="login-header">
                <div class="login-title">欢迎回来</div>
                <div class="login-sub">请选择您的登录方式</div>
                <a class="home-btn" href="./">
                    <i class="fas fa-home"></i>
                </a>
            </div>
            
            <div class="login-tabs">
                <div class="login-tab active" data-tab="social">社交登录</div>
            </div>
            
            <div class="login-content">
                <!-- 社交登录 -->
                <div class="tab-content active" id="social-tab">
                    <div class="social-login">
                        <div class="social-btn qq" data-type="qq" title="QQ登录">
                            <i class="fab fa-qq"></i>
                        </div>
                        <div class="social-btn wx" data-type="wx" title="微信登录">
                            <i class="fab fa-weixin"></i>
                        </div>
                    </div>
                    <div class="social-label">点击上方图标进行快捷登录，新用户将自动注册账号</div>
                    
                    <div class="form-group">
                        <button class="login-btn" id="social-login-btn">快速登录</button>
                    </div>
                </div>
                
                
            </div>
            
            <div class="login-footer">
                © 2025 <?php echo $conf['name']; ?> 版权所有 | 隐私政策 | 服务条款
            </div>
        </div>
    </div>

    <!-- 添加jQuery库 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/layer/2.3/layer.js"></script>
    <script>
        // 社交按钮点击事件
        document.querySelectorAll('.social-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                // 添加点击效果
                this.style.transform = 'scale(0.95)';
                setTimeout(() => {
                    this.style.transform = '';
                }, 200);
                
                // 从data-type属性获取登录类型
                const type = this.getAttribute('data-type');
                
                // 调用connect函数
                if (type) {
                    connect(type);
                }
            });
        });

        // 快速登录按钮点击事件
        document.getElementById('social-login-btn').addEventListener('click', function() {
            layer.msg('请选择一种社交登录方式', {icon: 0, time: 2000});
        });

        // 将connect函数定义在外部
        function connect(type) {
            var ii = layer.load(2, {shade:[0.1,'#fff']});
            $.ajax({
                type: "POST",
                url: "login.php?act=connect",
                data: {type: type},
                dataType: 'json',
                success: function(data) {
                    layer.close(ii);
                    if(data.code == 0){
                        window.location.href = data.url;
                    }else{
                        layer.alert(data.msg, {icon: 7});
                    }
                },
                error: function(){
                    layer.close(ii);
                    layer.alert('接口请求失败，请稍后重试', {icon: 2});
                }
            });
        }
    </script>
</body>
</html>